package com.fr.web.platform.entry;

import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.schedule.Schedulable;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.ObjectMapper;
import com.fr.schedule.dao.ValueFieldMapper;
import com.fr.schedule.job.ViewReportletJob;
import com.fr.schedule.job.WriteReportletJob;
import com.fr.schedule.task.ScheduleTask;
import com.fr.schedule.trigger.ITrigger;
import com.fr.third.org.quartz.JobDataMap;
import com.fr.third.org.quartz.JobDetail;
import com.fr.third.org.quartz.Scheduler;
import com.fr.third.org.quartz.SchedulerException;
import com.fr.web.platform.TransmitParameters;

public class ReportletEntry extends BaseEntry
  implements Schedulable
{
  public static final String TYPE_PREFIX = "2";
  public static final String TABLE_NAME = "fr_reportletentry";
  public static final String TRANSMITPARAMETERS = "transmitParameters";
  public static final String ISVIEW = "isView";
  public static final String REPORTLETPATH = "reportletPath";
  public static final int TRANSMITPARAMETERSTYPE = 12;
  public static final int ISVIEWTYPE = 4;
  public static final int REPORTLETPATHTYPE = 12;
  private boolean isView;
  private String reportletPath;
  private TransmitParameters transmitParameters;
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_reportletentry", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("parentId", 4, "parent"), new FieldMapper("displayName", 12, "name"), new FieldMapper("description", 12, "description"), new FieldMapper("reportletPath", 12, "reportletPath"), new ValueFieldMapper("isView", 4, "isView")
  {
    public Object field2Value(Object paramObject)
    {
      if (Boolean.FALSE.equals(paramObject))
        return new Integer(0);
      return new Integer(1);
    }

    public Object value2Field(Object paramObject)
    {
      if (paramObject == null)
        return Boolean.TRUE;
      Number localNumber = (Number)paramObject;
      if (localNumber.intValue() == 0)
        return Boolean.FALSE;
      return Boolean.TRUE;
    }
  }
  , new ValueFieldMapper("transmitParameters", 12, "transmitParameters")
  {
    public Object value2Field(Object paramObject)
    {
      String str = (String)paramObject;
      if (StringUtils.isBlank(str))
        return null;
      TransmitParameters localTransmitParameters = null;
      try
      {
        localTransmitParameters = TransmitParameters.parseJSON(new JSONArray(str));
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
      return localTransmitParameters;
    }

    public Object field2Value(Object paramObject)
    {
      if (paramObject == null)
        return "";
      TransmitParameters localTransmitParameters = (TransmitParameters)paramObject;
      String str = "";
      try
      {
        str = localTransmitParameters.createJSON().toString();
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
      return str;
    }
  }
   });

  public ReportletEntry()
  {
    this("");
  }

  public ReportletEntry(String paramString)
  {
    this.isView = true;
    this.reportletPath = null;
    this.transmitParameters = null;
    setReportletPath(paramString);
  }

  public ReportletEntry(String paramString1, String paramString2, String paramString3)
  {
    super(paramString1, paramString2);
    this.isView = true;
    this.reportletPath = null;
    this.transmitParameters = null;
    this.reportletPath = paramString3;
  }

  public JSONObject createJSONConfig()
    throws JSONException
  {
    JSONObject localJSONObject = super.createJSONConfig();
    localJSONObject.put("isView", isView());
    localJSONObject.put("path", getReportletPath());
    if ((this.transmitParameters != null) && (this.transmitParameters.size() > 0))
      localJSONObject.put("transmitParameters", this.transmitParameters.createJSON());
    return localJSONObject;
  }

  public void schedule(Scheduler paramScheduler, ScheduleTask paramScheduleTask)
    throws SchedulerException
  {
    JobDetail localJobDetail = new JobDetail(paramScheduleTask.getName(), getDisplayName(), getReportJobClass());
    localJobDetail.setDescription(paramScheduleTask.getDescription());
    localJobDetail.getJobDataMap().put("__task__", paramScheduleTask.getId());
    localJobDetail.getJobDataMap().put("__reportlet__", getId());
    paramScheduler.scheduleJob(localJobDetail, paramScheduleTask.getITrigger().createTrigger());
  }

  public Class getReportJobClass()
  {
    return WriteReportletJob.class;
  }

  public void setView(boolean paramBoolean)
  {
    this.isView = paramBoolean;
  }

  public boolean isView()
  {
    return this.isView;
  }

  public void setReportletPath(String paramString)
  {
    this.reportletPath = paramString;
  }

  public String getReportletPath()
  {
    return this.reportletPath;
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof ReportletEntry))
      return false;
    if (!(super.equals(paramObject)))
      return false;
    ReportletEntry localReportletEntry = (ReportletEntry)paramObject;
    return ((ComparatorUtils.equals(localReportletEntry.getReportletPath(), getReportletPath())) && (localReportletEntry.isView == this.isView));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ReportletEntry localReportletEntry = (ReportletEntry)super.clone();
    return localReportletEntry;
  }

  public String getTypePrefix()
  {
    return "2";
  }

  public TransmitParameters getTransmitParameters()
  {
    return this.transmitParameters;
  }

  public void setTransmitParameters(TransmitParameters paramTransmitParameters)
  {
    this.transmitParameters = paramTransmitParameters;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws JSONException
  {
    super.parseJSON(paramJSONObject);
    setReportletPath(paramJSONObject.getString("path"));
    setView(paramJSONObject.getBoolean("isView"));
    JSONArray localJSONArray = null;
    try
    {
      localJSONArray = paramJSONObject.getJSONArray("transmitParameters");
    }
    catch (Exception localException)
    {
    }
    if (localJSONArray != null)
      setTransmitParameters(TransmitParameters.parseJSON(localJSONArray));
  }
}